package com.cskaoyan.service.admin;

import com.cskaoyan.bean.*;

import com.cskaoyan.mapper.*;
import com.cskaoyan.model.admin.bo.GoodsCreateBo;
import com.cskaoyan.model.admin.bo.ListBo;
import com.cskaoyan.model.admin.vo.BaseData;
import com.cskaoyan.model.admin.vo.GoodsDetailVo;
import com.cskaoyan.model.admin.bo.*;
import com.cskaoyan.model.admin.vo.CatAndBrandVo;
import com.cskaoyan.model.admin.vo.CatAndBrandVo2;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class GoodsAdminGoodsServiceImpl implements GoodsAdminGoodsService {
    @Autowired
    MarketGoodsMapper marketGoodsMapper;

    @Autowired
    MarketCategoryMapper marketCategoryMapper;
    @Autowired
    MarketGoodsAttributeMapper marketGoodsAttributeMapper;
    @Autowired
    MarketGoodsProductMapper marketGoodsProductMapper;
    @Autowired
    MarketGoodsSpecificationMapper marketGoodsSpecificationMapper;
    @Autowired
    MarketBrandMapper marketBrandMapper;

    @Override
    public BaseData list(ListBo listBo, String goodsSn, String name, Integer goodsId) {
        MarketGoodsExample marketGoodsExample1 = new MarketGoodsExample();
        if (listBo.getPage() == null) {
            List<MarketGoods> marketGoods = marketGoodsMapper.selectByExample(marketGoodsExample1);
            long count = marketGoodsMapper.countByExample(marketGoodsExample1);
            BaseData baseData = new BaseData();
            baseData.setList(marketGoods);
            baseData.setPage(0);
            baseData.setPages(0);
            baseData.setTotal((int) count);
            return baseData;
        }
        PageHelper.startPage(listBo.getPage(), listBo.getLimit());//查询前分页

        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = marketGoodsExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if ((goodsSn != null && goodsSn.length() > 0) || goodsId != null || (name != null && name.length() > 0)) {
            if (goodsSn != null && goodsSn.length() > 0 ) {

                criteria.andGoodsSnEqualTo(goodsSn);
            }
            if (goodsId != null ) {
                criteria.andIdEqualTo(goodsId);
            }
            if (name != null && name.length() > 0) {
                criteria.andNameLike("%" + name + "%");
            }

        }
        marketGoodsExample.setOrderByClause(listBo.getSort() + " " + listBo.getOrder());//按规定排序
        List<MarketGoodsCopy> goodsList = marketGoodsMapper.selectByExample1(marketGoodsExample);//把条件塞进去（参数）

        PageInfo pageInfo = new PageInfo(goodsList);
        long total = pageInfo.getTotal();
        Integer pages = pageInfo.getPages();

        return BaseData.list(goodsList, (int) total, listBo.getPage(), listBo.getLimit(), pages);
    }

    @Transactional
    @Override
    public Integer create(GoodsCreateBo goodsCreateBo) {
//        GoodsEntityOfGoodsCreateBO goods = goodsCreateBo.getGoods();
//        List<AttributesEntityOfGoodsCreateBO> attributes = goodsCreateBo.getAttributes();
//        List<ProductsEntityOfGoodsCreateBO> products = goodsCreateBo.getProducts();
//        List<SpecificationsEntityOfGoodsCreateBO> specifications = goodsCreateBo.getSpecifications();

//        int row4 = marketGoodsAttributeMapper.insertSelective((MarketGoodsAttribute) attributes);
//        int row3 = marketGoodsProductMapper.insertSelective((MarketGoodsProduct) products);
//        int row2 = marketGoodsSpecificationMapper.insertSelective((MarketGoodsSpecification) specifications);


        //插入MarketGoods
        MarketGoods marketGoods = goodsCreateBo.getGoods().getMarketGoods();
        int affectedRows1 = marketGoodsMapper.insertSelective(marketGoods);
        System.out.println(affectedRows1 + ":插入marketGoods--------------------------------------");
        System.out.println(marketGoods.getId());

        //插入attributes
        List<MarketGoodsAttribute> attributes = goodsCreateBo.getAttributes();
        for (MarketGoodsAttribute attribute : attributes) {
            attribute.setGoodsId(marketGoods.getId());
            marketGoodsAttributeMapper.insertSelective(attribute);
            System.out.println(affectedRows1 + ":插入marketGoodsAttribute--------------------------------------");
        }

        //插入MarketGoodsSpecification
        List<MarketGoodsSpecification> specifications = goodsCreateBo.getSpecifications();
        for (MarketGoodsSpecification specification : specifications) {
            specification.setGoodsId(marketGoods.getId());
            System.out.println(marketGoods.getId());
            System.out.println(specification.getGoodsId());
            marketGoodsSpecificationMapper.insertSelective(specification);
            System.out.println(affectedRows1 + ":插入marketSpecification--------------------------------------");
        }

        //插入MarketProduct
        List<ProductsEntityOfGoodsCreateBO> products = goodsCreateBo.getProducts();
        for (ProductsEntityOfGoodsCreateBO product : products) {
            MarketGoodsProduct marketGoodsProduct = product.getMarketGoodsProduct();
            marketGoodsProduct.setGoodsId(marketGoods.getId());
            marketGoodsProductMapper.insertSelective(marketGoodsProduct);
            System.out.println(affectedRows1 + ":插入marketGoodsProduct--------------------------------------");
        }


        return 1;
    }

    @Override
    public CatAndBrandVo catAndBrand() {
        CatAndBrandVo catAndBrandVo = new CatAndBrandVo();
        List<BrandListEntity> list = marketBrandMapper.select(0);
        List<CategoryListEntity> list2 = marketCategoryMapper.selectIdAndNameByGroupPid();
        for (CategoryListEntity categoryListEntity : list2) {
            List<ChildrenEntity> list3 = marketCategoryMapper.selectByChildrenEntity(categoryListEntity);
            categoryListEntity.setChildren(list3);
        }
        catAndBrandVo.setBrandList(list);
        catAndBrandVo.setCategoryList(list2);
        return catAndBrandVo;
    }

    @Override
    public CatAndBrandVo2 catAndBrand2() {
        CatAndBrandVo2 catAndBrandVo2 = new CatAndBrandVo2();
        MarketBrandExample marketBrandExample = new MarketBrandExample();
        marketBrandExample.createCriteria().andIdIsNotNull();
        List<CatAndBrandVo2.BrandListBean> list1 = marketBrandMapper.selectBrandListBean(marketBrandExample);
        List<CatAndBrandVo2.CategoryListBean> list2 = marketCategoryMapper.selectNameAndIdByPid();
        for (CatAndBrandVo2.CategoryListBean categoryListBean : list2) {
            List<CatAndBrandVo2.CategoryListBean.ChildrenBean> list3 = marketCategoryMapper.selectChildrenBeanByPid(categoryListBean.getValue());
            categoryListBean.setChildren(list3);
        }
        catAndBrandVo2.setBrandList(list1);
        catAndBrandVo2.setCategoryList(list2);
        return catAndBrandVo2;
    }

    @Transactional
    @Override
    public Integer update(GoodsUpdateBO goodsUpdateBO) {

        //先更新goods
        GoodsUpdateBO.GoodsEntity goods = goodsUpdateBO.getGoods();
        int rows = marketGoodsMapper.updateByGoodsBO(goods);

        //更新attribute
        List<GoodsUpdateBO.AttributesEntity> attributes = goodsUpdateBO.getAttributes();
        for (GoodsUpdateBO.AttributesEntity attribute : attributes) {
            if (attribute.getId() == null || attribute.getId().equals("")) {
                marketGoodsAttributeMapper.insertByGoodsAttribute(attribute, goods.getId());
            } else {
                marketGoodsAttributeMapper.updateByGoodsAttribute(attribute);
            }

        }

        //更新specification
        List<GoodsUpdateBO.SpecificationsEntity> specifications = goodsUpdateBO.getSpecifications();
        for (GoodsUpdateBO.SpecificationsEntity specification : specifications) {
            marketGoodsSpecificationMapper.updateByGoodsBO(specification);
        }

        //更新
        List<GoodsUpdateBO.ProductsEntity> products = goodsUpdateBO.getProducts();
        for (GoodsUpdateBO.ProductsEntity product : products) {
            marketGoodsProductMapper.updateByGoodsBOProduct(product);
        }


        return 0;
    }
@Transactional
    @Override
    public Integer delete(Integer goodId) {
        int row = marketGoodsMapper.updateDelete(goodId);
        int row2 = marketGoodsProductMapper.updateDelete(goodId);
        int row3 = marketGoodsAttributeMapper.updateDelete(goodId);
        int row4 = marketGoodsSpecificationMapper.updateDelete(goodId);
        if(row==0 || row2==0 || row3 ==0 || row4==0){
        return -1;
    }

        return 1;
    }

    @Override
    public GoodsDetailVo detail(Integer id) {
        List<MarketCategory> categoryList;
        ArrayList<Integer> categoryIds = new ArrayList<>();
        MarketGoodsCopy marketGoodsCopy=marketGoodsMapper.selectByPrimaryKey1(id);

        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        marketCategoryExample.createCriteria().andPidEqualTo(marketGoodsCopy.getCategoryId());
        categoryList = marketCategoryMapper.selectByExample(marketCategoryExample);
        for (MarketCategory marketCategory : categoryList) {
           categoryIds.add(marketCategory.getId());
        }

        MarketGoodsAttributeExample marketGoodsAttributeExample = new MarketGoodsAttributeExample();
        marketGoodsAttributeExample.createCriteria().andGoodsIdEqualTo(marketGoodsCopy.getId());
        List<MarketGoodsAttribute> marketGoodsAttributes = marketGoodsAttributeMapper.selectByExample(marketGoodsAttributeExample);

        MarketGoodsProductExample marketGoodsProductExample = new MarketGoodsProductExample();
        marketGoodsProductExample.createCriteria().andGoodsIdEqualTo(marketGoodsCopy.getId());
        List<MarketGoodsProduct> marketGoodsProducts = marketGoodsProductMapper.selectByExample(marketGoodsProductExample);

        MarketGoodsSpecificationExample marketGoodsSpecificationExample = new MarketGoodsSpecificationExample();
        marketGoodsSpecificationExample.createCriteria().andGoodsIdEqualTo(marketGoodsCopy.getId());
        List<MarketGoodsSpecification> marketGoodsSpecifications = marketGoodsSpecificationMapper.selectByExample(marketGoodsSpecificationExample);

        GoodsDetailVo goodsDetailVo = new GoodsDetailVo();
        goodsDetailVo.setGoods(marketGoodsCopy);
        goodsDetailVo.setCategoryIds(categoryIds);
        goodsDetailVo.setAttributes(marketGoodsAttributes);
        goodsDetailVo.setProducts(marketGoodsProducts);
        goodsDetailVo.setSpecifications(marketGoodsSpecifications);
        return goodsDetailVo;
    }


}
